﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Web.Mvc.Paging;
using Kooboo.Globalization;

namespace Kooboo.Web.Areas.Commerce.Controllers
{
    using Kooboo.Commerce;
    
    using Kooboo.Web.Areas.Commerce.Binders;
    using Kooboo.IoC;
    
    using Kooboo.Web.Mvc;
    using Kooboo.Commerce.Contexts.Countries;
    using Kooboo.Commerce.Contexts.Taxes;

    public class TaxController : ManagementController
    {
        #region Index


        public ActionResult Index()
        {
            ViewData["delete_action"] = this.Url.Action<TaxController>(i => i.Delete(null));
            ViewData["add_action"] = this.Url.Action<TaxController>(i => i.Create());
            ViewData["edit_action"] = this.Url.Action<TaxController>(i => i.Edit(null));

            ViewData["Countries"] = this.Actor.QueryCountries().ToSelectableList();

            ViewData["Taxes"] = this.Actor
                .QueryTaxes()
                .OrderByDescending(i => i.Id)
                .Select(i => new
                {
                    i.Id,
                    i.Name,
                    Percent = i.Percent,
                    SubTaxes = i.SubTaxes.Select(m=>new
                    {
                        m.Id,
                        CountryCode = m.Country.CountryCode,
                        CountryName = m.Country.CountryName,
                        m.State,
                        m.Percent
                    })
                })
                .ToArray()
                .SerializeToJson();

            return View();
        }

        #endregion

        #region Create

        public ActionResult Create()
        {
            var tax = ObjectContainer.CreateInstance<ITax>();
            return View(tax);
        }

        [HttpPost]
        public ActionResult Create(ITax tax)
        {
            if (ModelState.IsValid)
            {
                this.Actor.Post(tax);

                return Json(new
                {
                    Data = new
                    {
                        tax.Id,
                        tax.Name,
                        Percent = tax.Percent,
                        SubTaxes = tax.SubTaxes.Select(m => new
                        {
                            m.Id,
                            CountryCode = m.Country.CountryCode,
                            CountryName = m.Country.CountryName,
                            m.State,
                            m.Percent
                        })
                    }
                });
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
        }

        #endregion

        #region Edit

        public ActionResult Edit(int id)
        {
            var tax = this.Actor
                .QueryTaxes()
                .FirstOrDefault(i => i.Id == id);

            if (tax == null)
                return HttpNotFound();

            return View(tax);
        }

        [HttpPost]
        public ActionResult Edit(ITax tax)
        {
            if (ModelState.IsValid)
            {
                this.Actor.Put(tax);

                return Json(new
                {
                    Data = new
                    {
                        tax.Id,
                        tax.Name,
                        Percent = tax.Percent,
                        SubTaxes = tax.SubTaxes.Select(m => new
                        {
                            m.Id,
                            CountryCode = m.Country.CountryCode,
                            CountryName = m.Country.CountryName,
                            m.State,
                            m.Percent
                        })
                    }
                });
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
        }

        #endregion

        #region Delete

        [HttpPost]
        public ActionResult Delete(ITax tax)
        {
            this.Actor.Delete(tax);
            return Json(true);
        }

        #endregion


    }
}
